package hu.myai.triggers.bearish;

import hu.myai.api.tick.AbstractTickProvider;
import hu.myai.api.triggers.AbstractTrigger;
import hu.myai.indicators.tick.TickPositionChangeIndicator;
import hu.myai.indicators.tick.TickShadowRateIndicator;
import hu.myai.indicators.tick.TickVolumeChangeIndicator;
import hu.myai.model.IndicatorVO;
import hu.myai.model.TriggerVO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A bull is killed when a candle started to raise with volume, but than strong
 * sell appear and consume all the raise.
 * <ul>
 * <li>Upper candle
 * <li>high volume
 * <li>big upper shadow
 * </ul>
 * 
 * @author bozbalint
 * */
public class KilledBullTrigger extends AbstractTrigger {

    /**
     * The object used when logging debug,errors,warnings and info.
     * */
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    private String volumeID;
    private String shadowID;
    private String positionID;

    public KilledBullTrigger(String name, AbstractTickProvider tick) {
        super(name, tick);
        volumeID = register(new TickVolumeChangeIndicator());
        shadowID = register(new TickShadowRateIndicator(TickShadowRateIndicator.UPPERSHADOW_TO_ALL));
        positionID = register(new TickPositionChangeIndicator());
    }


    @Override
    protected TriggerVO triggerCalculation() {

        IndicatorVO volumeIndicator = indicatorMap.get(volumeID);
        IndicatorVO shadowIndicator = indicatorMap.get(shadowID);
        IndicatorVO positionIndicator = indicatorMap.get(positionID);

        // ignore calculation if no indicator values
        if (volumeIndicator == null || shadowIndicator == null) {
            return null;
        }

        TriggerVO trigger = null;

        // high volume
        if (volumeIndicator.getValue() > 2.3)

            // big upper shadow
            if (shadowIndicator.getValue() > 0.4)

                // higher position
                if (positionIndicator.getValue() > 1) {
                    trigger = new TriggerVO();
                    trigger.setTick(lastTick);
                    trigger.setRisk(20);
                    trigger.setTradeType(TriggerVO.BUY_ONMARKET);
                    log.debug(trigger.toString());
                }

        return trigger;
    }
}
